package com.xingji.caixia.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.widget.RecyclerView.Adapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xingji.caixia.R;
import com.xingji.caixia.entitys.MainData;
import com.xingji.caixia.entitys.UploadData;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;


/**
 * Created by fengm on 2017-7-22.
 */

public class DataAdapter extends Adapter<DataAdapter.MyViewHolder> {


    private final SimpleDateFormat formatter;
    Context mcontext;
    List<UploadData> datalist;

    public void refresh(List<UploadData> list){
        this.datalist = list;
        this.notifyDataSetChanged();
    }

    public DataAdapter(Context context, List<UploadData> datalist) {
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        mcontext = context;
        this.datalist= datalist;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mcontext).inflate(R.layout.item_layout, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        UploadData data = datalist.get(position);
        holder.cardno.setText(data.getCard());
        holder.time.setText(formatter.format(data.getTime()));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Bitmap bm = BitmapFactory.decodeFile(data.getThumbImgPath(), options);
        holder.image.setImageBitmap(bm);

        if (data.getUploaded()){
            if (data.getSad()){
                holder.status.setImageResource(R.mipmap.suc);
            }else{
                holder.status.setImageResource(R.mipmap.warn);
            }
        }else{
            holder.status.setImageResource(R.mipmap.err);
        }
        //
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemclicklintener.Itemclick(view,position);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemclicklintener.Itemclick(view,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    private OnItemclicklintener itemclicklintener;

    public void setOnItemclickLintener(OnItemclicklintener itemclicklintener) {
        this.itemclicklintener = itemclicklintener;
    }

    public interface OnItemclicklintener {
        void Itemclick(View view, int position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView time, cardno;
        ImageView image,status;

        MyViewHolder(View itemView) {
            super(itemView);
            time = (TextView) itemView.findViewById(R.id.time);
            cardno = (TextView) itemView.findViewById(R.id.cardno);
            image = (ImageView) itemView.findViewById(R.id.image);
            status = (ImageView) itemView.findViewById(R.id.status);
        }


    }

}
